package interpreter.bytecodes;

import interpreter.VirtualMachine;
import java.util.NoSuchElementException;
import java.util.Vector;

/**
 *
 * @author Kristofer Selbekk
 */
public class LoadByteCode extends ByteCode {

    private int offset;
    private String id;

    /**
     * Initializes the byte code with no arguments
     */
    public void init( Vector<String> args ) {
        try {
            offset = Integer.parseInt(args.get(0));
            id = args.get(1);
        } catch( NumberFormatException nfe ) {
            // If bad format
            System.out.println("Bad format!");
            System.exit(-1);
        } catch( NoSuchElementException nse ) {
            // If empty argument
            System.out.println("Empty vector!");
            System.exit(-1);
        } catch( NullPointerException npe ) {
            // If argument missing
            System.out.println("Argument given = null.");
            System.exit(-1);
        }
    }

    /**
     * Action: Loads an element already on the stack to the top of the stack
     *         for further processing
     *
     * @param rt
     */
    public void exec( VirtualMachine rt ) {
        rt.load( offset );
    }

    public Object getArgs() {
        return offset + " " + id;
    }

    public String toString() {
        return super.toString() + "\t<load "+id+">";
    }
}
